package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_jl.domain.ReportJlCjlzpznfxjlbBaDetail;
import com.ruoyi.report_jl.domain.ReportJlXjlgsjlbBaDetail;
import com.ruoyi.report_jl.domain.vo.ReportJlXjlgsjlbBaDetailVo;
import com.ruoyi.report_jl.mapper.ReportJlXjlgsjlbBaDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlXjlgsjlbBsInfoBo;
import com.ruoyi.report_jl.domain.vo.ReportJlXjlgsjlbBsInfoVo;
import com.ruoyi.report_jl.domain.ReportJlXjlgsjlbBsInfo;
import com.ruoyi.report_jl.mapper.ReportJlXjlgsjlbBsInfoMapper;
import com.ruoyi.report_jl.service.IReportJlXjlgsjlbBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 细集料筛分试验检测记录（干筛法）Service业务层处理
 *
 * @author tll
 * @date 2024-08-05
 */
@RequiredArgsConstructor
@Service
public class ReportJlXjlgsjlbBsInfoServiceImpl implements IReportJlXjlgsjlbBsInfoService {

    private final ReportJlXjlgsjlbBsInfoMapper baseMapper;
    private final ReportJlXjlgsjlbBaDetailMapper reportJlXjlgsjlbBaDetailMapper;

    /**
     * 查询细集料筛分试验检测记录（干筛法）
     */
    @Override
    @FIllBasicInfo
    public ReportJlXjlgsjlbBsInfoVo queryById(Long excelId){
        // 主表
        ReportJlXjlgsjlbBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 子表
        LambdaQueryWrapper<ReportJlXjlgsjlbBaDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(excelId != null, ReportJlXjlgsjlbBaDetail::getExcelId, excelId);
        lqw.orderByAsc(ReportJlXjlgsjlbBaDetail::getCreateTime);
        List<ReportJlXjlgsjlbBaDetailVo> detailList = reportJlXjlgsjlbBaDetailMapper.selectVoList(lqw);
        vo.setDetailList(detailList);
        return vo;
    }

    /**
     * 查询细集料筛分试验检测记录（干筛法）列表
     */
    @Override
    public TableDataInfo<ReportJlXjlgsjlbBsInfoVo> queryPageList(ReportJlXjlgsjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlXjlgsjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportJlXjlgsjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询细集料筛分试验检测记录（干筛法）列表
     */
    @Override
    public List<ReportJlXjlgsjlbBsInfoVo> queryList(ReportJlXjlgsjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportJlXjlgsjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlXjlgsjlbBsInfo> buildQueryWrapper(ReportJlXjlgsjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlXjlgsjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportJlXjlgsjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportJlXjlgsjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportJlXjlgsjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportJlXjlgsjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportJlXjlgsjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportJlXjlgsjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportJlXjlgsjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportJlXjlgsjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportJlXjlgsjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportJlXjlgsjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportJlXjlgsjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportJlXjlgsjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportJlXjlgsjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportJlXjlgsjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportJlXjlgsjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportJlXjlgsjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportJlXjlgsjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportJlXjlgsjlbBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportJlXjlgsjlbBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportJlXjlgsjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportJlXjlgsjlbBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportJlXjlgsjlbBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportJlXjlgsjlbBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportJlXjlgsjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(bo.getShaifhzzlItem1() != null, ReportJlXjlgsjlbBsInfo::getShaifhzzlItem1, bo.getShaifhzzlItem1());
        lqw.eq(bo.getSunhItem1() != null, ReportJlXjlgsjlbBsInfo::getSunhItem1, bo.getSunhItem1());
        lqw.eq(bo.getSunhlItem1() != null, ReportJlXjlgsjlbBsInfo::getSunhlItem1, bo.getSunhlItem1());
        lqw.eq(bo.getShaifhzzlItem2() != null, ReportJlXjlgsjlbBsInfo::getShaifhzzlItem2, bo.getShaifhzzlItem2());
        lqw.eq(bo.getSunhItem2() != null, ReportJlXjlgsjlbBsInfo::getSunhItem2, bo.getSunhItem2());
        lqw.eq(bo.getSunhlItem2() != null, ReportJlXjlgsjlbBsInfo::getSunhlItem2, bo.getSunhlItem2());
        lqw.eq(StringUtils.isNotBlank(bo.getChenglcsItem1()), ReportJlXjlgsjlbBsInfo::getChenglcsItem1, bo.getChenglcsItem1());
        lqw.eq(bo.getZhilItem1() != null, ReportJlXjlgsjlbBsInfo::getZhilItem1, bo.getZhilItem1());
        lqw.eq(StringUtils.isNotBlank(bo.getChenglcsItem2()), ReportJlXjlgsjlbBsInfo::getChenglcsItem2, bo.getChenglcsItem2());
        lqw.eq(bo.getZhilItem2() != null, ReportJlXjlgsjlbBsInfo::getZhilItem2, bo.getZhilItem2());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportJlXjlgsjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportJlXjlgsjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportJlXjlgsjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        return lqw;
    }

    /**
     * 新增细集料筛分试验检测记录（干筛法）
     */
    @Override
    public Boolean insertByBo(ReportJlXjlgsjlbBsInfoBo bo) {
        ReportJlXjlgsjlbBsInfo add = BeanUtil.toBean(bo, ReportJlXjlgsjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改细集料筛分试验检测记录（干筛法）
     */
    @Override
    public Boolean updateByBo(ReportJlXjlgsjlbBsInfoBo bo) {
        ReportJlXjlgsjlbBsInfo update = BeanUtil.toBean(bo, ReportJlXjlgsjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            reportJlXjlgsjlbBaDetailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), ReportJlXjlgsjlbBaDetail.class));
        }
        return flag;
//        boolean upd = baseMapper.updateById(update) > 0;
//        boolean upd2 = false;
//        boolean insert = false;
//        if (ObjectUtils.isNotEmpty(bo.getDetailList())) {
//            // 根据excelId查询明细
//            QueryWrapper<ReportJlXjlgsjlbBaDetail> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("excel_id", bo.getExcelId());
//            List<ReportJlXjlgsjlbBaDetail> list = reportJlXjlgsjlbBaDetailMapper.selectList(queryWrapper);
//            for (ReportJlXjlgsjlbBaDetail det : bo.getDetailList()) {
//                if (ObjectUtils.isEmpty(list)) {
//                    // 新增
//                    insert = reportJlXjlgsjlbBaDetailMapper.insert(det) > 0;
//                } else if (ObjectUtils.isNotEmpty(det.getDetailId())) {
//                    // 修改
//                    upd2 = reportJlXjlgsjlbBaDetailMapper.updateById(det) > 0;
//                } else {
//                    throw new ServiceException("更新主键不能为空, 请刷新后重试!");
//                }
//            }
//        }
//        return upd && upd2 && insert;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlXjlgsjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除细集料筛分试验检测记录（干筛法）
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
